package westh.ilib.service.surveyReport.documentSearch;

import java.io.File;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

import java.util.Date;

/**
 * local dataset document indexer
 * @author Zhang Yanxing
 * 2010-8-19
 */
public class Indexer {
	public static void index(String xmlPath, String indexPath) {
		// dataset directory
		final File docDir = new File(xmlPath);
		
		// index directory
		final File indexDir = new File(indexPath);

		if (!docDir.exists() || !docDir.canRead()) {
			System.out
					.println("Document directory '"
							+ docDir.getAbsolutePath()
							+ "' does not exist or is not readable, please check the path");
			System.exit(1);
		}

		try {
			Date start = new Date();
			IndexWriter writer;
			
			// if index exists, incremental index
			if(indexDir.exists()){
				writer = new IndexWriter(FSDirectory.open(indexDir),
						new StandardAnalyzer(Version.LUCENE_CURRENT), false,
						IndexWriter.MaxFieldLength.LIMITED);
			}
			// create a new index
			else{
				writer = new IndexWriter(FSDirectory.open(indexDir),
						new StandardAnalyzer(Version.LUCENE_CURRENT), true,
						IndexWriter.MaxFieldLength.LIMITED);
			}
			System.out.println("Indexing to directory '" + indexDir + "'...");

			if (!docDir.isDirectory()) {
				System.out.println("'" + xmlPath + "' is not a directory");
				writer.close();
				System.exit(1);
			}

			// get all files in data directory, 
			// call FileDocument.Document(File) to transform .xml into Lucene Document class
			// call addDocument to write into the index
			File[] files = docDir.listFiles();
			for (int i = 0; i < files.length; i++) {
				writer.addDocument(FileDocument.Document(files[i]));
			}

			// index optimize
			System.out.println("Optimizing...");
			writer.optimize();

			writer.close();

			Date end = new Date();
			System.out.println(end.getTime() - start.getTime()
					+ " total milliseconds");
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(" caught a " + e.getClass()
					+ "\n with message: " + e.getMessage());
		}
	}

	public static void main(String[] args) {
		// directory path of input data docs
		final String xmlPath = "E:\\zhyx\\SurveyReport\\xml\\";

		// directory path of index
		final String indexPath = "E:\\zhyx\\SurveyReport\\index\\";

		Indexer.index(xmlPath, indexPath);

		// unit test
		try {
			IndexReader reader = IndexReader.open(FSDirectory.open(new File(
					indexPath)), true); // only searching, so read-only=true

			System.out.println(reader.numDocs());
			for (int i = 0; i < reader.numDocs(); i++) {
				System.out.println(i);
				System.out.println(reader.document(i).getField("path"));
				System.out.println(reader.document(i).getField("year"));
				System.out.println(reader.document(i).getField("conf"));
				System.out.println(reader.document(i).getField("title"));
				System.out.println(reader.document(i).getField("keyword"));
				System.out.println(reader.document(i).getField("summary"));
				System.out.println(reader.document(i).getField("contents"));
				System.out.println();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
